home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 131_01.zip / ACRL.C < prev    next >
Text File  |  1993-06-05  |  6KB  |  352 lines

  1. /*
  2.     ***********************************************************
  3.     **            8080/8085 Assembler              **
  4.     **                             **
  5.     **        .CRL Output format for BDS CLINK         **
  6.     ***********************************************************
  7.  
  8.     W. Lemiszki                     9 Jan 1982
  9.  
  10.     Filename: acrl.c                BDS C v1.50
  11. */
  12.  
  13.  
  14. #define VER "2.0"            /* version number        */
  15. #define DEFEXT ".CSM"            /* default input file type    */
  16.  
  17. #include "acrl.h"
  18.  
  19.  
  20. /*
  21.  *    M A I N
  22.  *    -------
  23.  */
  24. main(argc,argv)
  25. int argc;
  26. char **argv;
  27. {
  28.     int i;
  29.  
  30.     init();
  31.     printf("\n\n8080/8085  .CRL Assembler  v%s\n\n", VER);
  32.  
  33.     if (argc == 1)
  34.         fatal("Usage:  acrl <sourcefile> [-p -o -v]\n");
  35.  
  36.     strcpy(iname, argv[1]);            /* source name */
  37.     if (index(iname, '.') == NULL)
  38.         strcat(iname, DEFEXT);        /* add extention */
  39.  
  40.     if (fopen(iname, infile) == ERROR)
  41.         fatal("Can't open %s\n", iname);
  42.  
  43.     for (i=2; i<argc; i++)            /* process options */
  44.         option(argv[i]);
  45.  
  46.     if (objflg)
  47.         {
  48.         strcpy(oname, iname);
  49.         strcpy(index(oname, '.'), ".CRL");    /* add output ext */
  50.         if (fcreat(oname, outfile) == ERROR)
  51.             fatal("Can't create %s\n", oname);
  52.         }
  53.  
  54.     assemble();
  55. }
  56.  
  57.  
  58.  
  59.  
  60. /*
  61.  *    Process optional arguments
  62.  *    --------------------------
  63.  */
  64. option(arg)
  65. char *arg;
  66. {
  67.     if (strcmp(arg, "-P") == 0)
  68.         printflg = TRUE;
  69.     else if (strcmp(arg, "-O") == 0)
  70.         objflg = FALSE;
  71.     else if (strcmp(arg, "-V") == 0)
  72.         verbose = TRUE;
  73.     else
  74.         printf("Ignoring unknown option: %s\n", arg);
  75. }
  76.  
  77.  
  78.  
  79. /* Fatal error exit */
  80. fatal(format, arg)
  81. char *format, *arg;
  82. {
  83.     printf("\n\7*** Error: ");        /* header */
  84.     printf(format, arg);            /* error msg */
  85.     exit();
  86. }
  87.  
  88.  
  89.  
  90. /* Find a char in a string */
  91. char *index(str, c)
  92. char *str, c;
  93. {
  94.     while (*str)
  95.         {
  96.         if (c == *str)
  97.             return (str);
  98.         ++str;
  99.         }
  100.     return NULL;
  101. }
  102.  
  103.  
  104.  
  105.  
  106. /*
  107.  *    Initialize
  108.  *    ----------
  109.  */
  110. init()
  111. {
  112.     initctab();                /* initialize the tables */
  113.     initsymb();
  114.     init8080();
  115.  
  116.     objflg = TRUE;                /* set the default options */
  117.     printflg = FALSE;
  118.     verbose = FALSE;
  119. }
  120.  
  121.  
  122.  
  123. /*
  124.  *    Assemble the Source File
  125.  *    ------------------------
  126.  */
  127. assemble()
  128. {
  129.     asm(0);                    /* do pass 1 */
  130.     putchar('\n');
  131.     fclose(infile);                /* rewind source file */
  132.     fopen(iname, infile);
  133.     asm(1);                    /* do pass 2 */
  134.  
  135.     fclose(infile);
  136.     if (objflg)
  137.         {
  138.         fflush(outfile);
  139.         fclose(outfile);
  140.         gendir();            /* output directory */
  141.         }
  142.  
  143.     if (errcnt)
  144.         printf("\n%4d", errcnt);
  145.         else
  146.         puts("\n  NO");
  147.     puts(" ASSEMBLY ERROR(S)\n");
  148. }
  149.  
  150.  
  151.  
  152.  
  153. /*
  154.  *    Perform a pass over the source
  155.  *    ------------------------------
  156.  */
  157. asm(p)
  158. int p;
  159. {
  160.     pass = p;
  161.     infunc = endflag = FALSE;        /* init for pass */
  162.     linecnt = errcnt = 0;
  163.     numfunc = 0;
  164.     outloc = 0;
  165.     lc = 0x205;
  166.  
  167.     while (!endflag)
  168.         {
  169.         if (kbhit())
  170.             getchar();        /* check for console abort */
  171.  
  172.         readline();            /* get a line */
  173.         parse();            /* parse it */
  174.         if (pass)            /* if pass 2 ... */
  175.             {
  176.             if (objflg)
  177.             gen();            /* code buffer out */
  178.             if (printflg)
  179.             list();            /* print listing */
  180.               else
  181.             ereport();        /* or just report errors */
  182.             }
  183.         lc += codelen;            /* adjust location cntrs */
  184.         if (prepost != PRE)
  185.             floc += codelen;
  186.         }
  187. }
  188.  
  189.  
  190.  
  191. /* Get the next source line */
  192. readline()
  193. {
  194.     ++linecnt;
  195.     if (incflag)                /* include file */
  196.         if ((lptr = fgets(line, incfile)) == 0)
  197.             {
  198.             fclose(incfile);
  199.             linecnt = ++linesav;
  200.             incflag = FALSE;
  201.             }
  202.     if (!incflag)                /* from input file */
  203.         if ((lptr = fgets(line, infile)) == 0)
  204.             fatal("Unexpected EOF\n");
  205. }
  206.  
  207.  
  208.  
  209.  
  210. /*
  211.  *    Code Buffer to output file
  212.  *    ---------------------------
  213.  */
  214. gen()
  215. {
  216.     int i;
  217.  
  218.     while (outloc < lc)            /* sync up */
  219.         {
  220.         eputc(0);
  221.         outloc++;
  222.         }
  223.     for (i=0; i<codelen; i++)        /* put each byte */
  224.         {
  225.         eputc(codebuf[i]);
  226.         outloc++;
  227.         }
  228. }
  229.  
  230.  
  231. /* Put a char with error checking */
  232. eputc(c)
  233. char c;
  234. {
  235.     if (putc(c, outfile) == ERROR)
  236.         fatal("Write error\n");
  237. }
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.  *    Report lines in error to the console
  244.  *    ------------------------------------
  245.  */
  246. ereport()
  247. {
  248.     if (errchar != ' ')
  249.         {
  250.         putchar(errchar);            /* error char */
  251.         if (incflag)
  252.         printf(" %4d : ", linesav);    /* include line # */
  253.         printf(" %4d\t%s", linecnt, line);    /* line# and line */
  254.         }
  255. }
  256.  
  257.  
  258.  
  259.  
  260. /*
  261.  *    Console listing
  262.  *    ---------------
  263.  */
  264. list()
  265. {
  266.     int i;
  267.  
  268.     i = 0;
  269.     putchar(errchar);            /* error char */
  270.     putchar(xrel ? '\'' : ' ');        /* reloc char */
  271.  
  272.     if (codelen && !prepost)
  273.         {
  274.         printf("%04x ", floc);        /* address */
  275.         i = listcode(i);        /* list up to 4 bytes */
  276.         putchar('\t');
  277.         }
  278.     else if (specflag)            /* list special value */
  279.         printf("%04x\t\t",specval);    /* list specval */
  280.     else
  281.         printf("\t\t");            /* nothing */
  282.  
  283.     puts(line);                /* list the line */
  284.     if (verbose)
  285.         while (i < codelen)        /* rest of code buffer */
  286.             {
  287.             puts("       ");
  288.             i = listcode(i);
  289.             putchar('\n');
  290.             }
  291. }
  292.  
  293.  
  294.  
  295. /* Print bytes in code field */
  296. listcode(i)
  297. int i;                    /* index into codebuf[] */
  298. {
  299.     int j;
  300.  
  301.     for(j = min(4, codelen - i);  j--;  )
  302.         printf("%02x", codebuf[i++]);
  303.     return i;
  304. }
  305.  
  306.  
  307.  
  308.  
  309. /*
  310.  *    Generate the directory
  311.  *    -----------------------
  312.  */
  313. gendir()
  314. {
  315.     int i, fd;
  316.  
  317.     fd = open(oname, 1);            /* reopen crl file */
  318.     codelen = 0;                /* empty the code buffer */
  319.     setmem(codebuf, 512, 0);        /* zero it */
  320.     if (numfunc)
  321.         funcwalk(roots[FUNCS]);        /* output the functions */
  322.     code(0x80);                /* null byte */
  323.     codew(lc);                /* next free address */
  324.     write(fd, codebuf, 4);
  325.     close(fd);
  326. }
  327.  
  328.  
  329. /* Output the function list */
  330. funcwalk(id)
  331. struct entry *id;
  332. {
  333.     if (id->next != NULL)
  334.         funcwalk(id->next);        /* (first is at end of list) */
  335.     nameout(id->nam);            /* name to code buffer */
  336.     codew(faddr[id->val]);            /* address */
  337. }
  338.  
  339.  
  340. /* Put function name to code buffer */
  341. nameout(name)
  342. char *name;
  343. {
  344.     int i;
  345.  
  346.     for (i = strlen(name) - 1;  i--;  )
  347.         code(*name++);
  348.     code(*name + 0x80);            /* set parity bit */
  349. }
  350.  
  351. /*EOF*/
  352.